import ProductModel from "../../../models/Product";
import Crypt from "../../../utils/Crypt";

const productModel = new ProductModel()

Page({
  data: {
    page: 0,
    pageSize: 10,
    list: [],
    busy: false,
    id: 0
  },

  onLoad: function (options) {
    this.setData({ id: options.id })
    this._getList()
  },

  onReachBottom() {
    this._getList(true)
  },

  _getList(append = false) {
    if (this.data.busy === false) {
      this.setData({
        busy: true,
        page: this.data.page + 1
      })
      productModel.getArea({ ...this.data }).then(response => {
        let { data } = response
        data = Crypt.parse(data)
        if (data.list.length === 0) {
          this.setData({ busy: true })
        } else {
          this.setData({ busy: false })
          if (append) {
            this.setData({ list: this.data.list.concat(data.list) })
          } else {
            this.setData({ list: data.list })
          }
        }
      })
    }
  }
});
